package com.ymy.thinking.in.spring.bean.instance;

import com.ymy.thinking.in.spring.bean.entity.Person;
import com.ymy.thinking.in.spring.bean.entity.PersonFactoryBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.ReflectionUtils;

/**
 * Bean 实例化示例
 *
 * @author Ringo
 * @date 2021/8/25 21:02
 */
public class BeanBasicInstanceDemo {

    public static void main(String[] args) {
        // 1: 创建 IoC 容器
        String configLocation = "classpath:META-INF/bean-instance-context.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocation);

        // 2: 依赖查找
        // 静态方法 实例化Bean
        Person personByStaticMethod = applicationContext.getBean("person-by-static-method", Person.class);
        // 实例方法 实例化Bean
        Person personByInstanceMethod = applicationContext.getBean("person-by-instance-method", Person.class);
        // FactoryBean 实例化Bean
        Person personByFactoryBean = applicationContext.getBean("person-by-factory-bean", Person.class);

        System.out.println(personByStaticMethod);
        System.out.println(personByInstanceMethod);
        System.out.println(personByFactoryBean);
        System.out.println(personByStaticMethod == personByInstanceMethod);     // false
        System.out.println(personByStaticMethod == personByFactoryBean);        // false
    }

}
